unit uClassesPesquisa;

interface

uses
 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, ExtCtrls, Grids, DBGrids, ComCtrls, uDM1, FMTBcd,
  Provider, DBClient, DB, SqlExpr , Contnrs, Mask , DBCtrls;


type
  TPesquisaLookup = class
    private
    FCampos: TObjectList;
    procedure SetCampos(const Value: TObjectList);
    procedure EventoKeyPressDBEdit (Sender: TObject; var Key: Char);
    //procedure EventoKeyPressDBEdit (Sender: TObject) overload;
    procedure EventoOnExitDBEdit (Sender : TObject);
    public
      property Campos : TObjectList read FCampos write SetCampos;
      procedure AddCampo ( vDBEdit : TDBEdit ; vEdit : TEdit ; vTabela , vApelidoTabela , vCampoChave , vCampoExibicao , vCaptionFormPesquisa : string ; cRegistraEventoOnExit , cRegistraEventoOnKeyPress : boolean );
      procedure RegistrarEventos;
      procedure PopularEDITs;
    published
      constructor Create;
      destructor Destroy;override;

  end;

  TPesquisaLookupCampo = class
    private
    FcEdti: TEdit;
    FTabela: string;
    FCampoChave: string;
    FApelidoTabela: string;
    FCampoExibicao: string;
    FcDBEdit: TDBEdit;
    FCaptionFormPesquisa: string;
    FRegistraEventoOnExit: Boolean;
    FRegistraEventoOnKeyPress: Boolean;
    procedure SetApelidoTabela(const Value: string);
    procedure SetCampoChave(const Value: string);
    procedure SetCampoExibicao(const Value: string);
    procedure SetCaptionFormPesquisa(const Value: string);
    procedure SetcDBEdit(const Value: TDBEdit);
    procedure SetcEdti(const Value: TEdit);
    procedure SetTabela(const Value: string);
    procedure SetRegistraEventoOnExit(const Value: Boolean);
    procedure SetRegistraEventoOnKeyPress(const Value: Boolean);

    public
      property cDBEdit : TDBEdit read FcDBEdit write SetcDBEdit;
      property cEdti : TEdit read FcEdti write SetcEdti;
      property Tabela : string read FTabela write SetTabela;
      property ApelidoTabela : string read FApelidoTabela write SetApelidoTabela;
      property CampoChave : string read FCampoChave write SetCampoChave;
      property CampoExibicao : string read FCampoExibicao write SetCampoExibicao;
      property CaptionFormPesquisa : string read FCaptionFormPesquisa write SetCaptionFormPesquisa;
      property RegistraEventoOnExit : Boolean read FRegistraEventoOnExit write SetRegistraEventoOnExit;
      property RegistraEventoOnKeyPress : Boolean read FRegistraEventoOnKeyPress write SetRegistraEventoOnKeyPress;
    published

  end;

  TFuncoesPesquisa = class
    public
      class procedure CriarFormPesquisa (vTabela , vTabelaApelido , vCampoRetorno , vCaptionFormPesquisa : string );
  end;



implementation

uses uFrmPesquisaPadrao2;

{ TPesquisaLookupCampo }

procedure TPesquisaLookupCampo.SetApelidoTabela(const Value: string);
begin
  FApelidoTabela := Value;
end;

procedure TPesquisaLookupCampo.SetCampoChave(const Value: string);
begin
  FCampoChave := Value;
end;

procedure TPesquisaLookupCampo.SetCampoExibicao(const Value: string);
begin
  FCampoExibicao := Value;
end;

procedure TPesquisaLookupCampo.SetCaptionFormPesquisa(const Value: string);
begin
  FCaptionFormPesquisa := Value;
end;

procedure TPesquisaLookupCampo.SetcDBEdit(const Value: TDBEdit);
begin
  FcDBEdit := Value;
end;

procedure TPesquisaLookupCampo.SetcEdti(const Value: TEdit);
begin
  FcEdti := Value;
end;

procedure TPesquisaLookupCampo.SetRegistraEventoOnExit(const Value: Boolean);
begin
  FRegistraEventoOnExit := Value;
end;

procedure TPesquisaLookupCampo.SetRegistraEventoOnKeyPress(
  const Value: Boolean);
begin
  FRegistraEventoOnKeyPress := Value;
end;

procedure TPesquisaLookupCampo.SetTabela(const Value: string);
begin
  FTabela := Value;
end;

{ TPesquisaLookup }



procedure TPesquisaLookup.AddCampo(vDBEdit: TDBEdit; vEdit: TEdit; vTabela,
  vApelidoTabela, vCampoChave, vCampoExibicao, vCaptionFormPesquisa: string ; cRegistraEventoOnExit , cRegistraEventoOnKeyPress : boolean);
var Campo : TPesquisaLookupCampo;
begin
  if not Assigned(Campos) then Campos := TObjectList.Create;
  Campo := TPesquisaLookupCampo.Create;
  Campo.cDBEdit := vDBEdit;
  Campo.cEdti := vEdit;
  Campo.Tabela := vTabela;
  Campo.ApelidoTabela := vApelidoTabela;
  Campo.CampoChave := vCampoChave;
  Campo.CampoExibicao := vCampoExibicao;
  Campo.CaptionFormPesquisa := vCaptionFormPesquisa;
  Campo.RegistraEventoOnExit := cRegistraEventoOnExit;
  Campo.RegistraEventoOnKeyPress := cRegistraEventoOnKeyPress;
  Campo.cEdti.ReadOnly := True;
  Campos.Add(Campo);
end;

procedure TPesquisaLookup.EventoKeyPressDBEdit(Sender: TObject; var Key: Char);
var i : Integer;
  sdsTemp : TSQLDataSet;
begin
  if Key in [#13,#43] then
  begin
  Key := #0;
  sdsTemp := TSQLDataSet.Create(nil);
  sdsTemp.SQLConnection := DM1.CONEXAO;
  for i := 0 to Campos.Count - 1 do
  begin
    if TPesquisaLookupCampo(Campos[i]).cDBEdit.Name = (Sender as TDBEdit).Name then
    begin
      if Trim(TPesquisaLookupCampo(Campos[i]).cDBEdit.Text) = '' then
      sdsTemp.CommandText := 'select first 0  ' + TPesquisaLookupCampo(Campos[i]).CampoExibicao +
      ' from ' + TPesquisaLookupCampo(Campos[i]).Tabela
      else
        sdsTemp.CommandText := 'select ' + TPesquisaLookupCampo(Campos[i]).CampoExibicao +
        ' from ' + TPesquisaLookupCampo(Campos[i]).Tabela + ' where ' +
        TPesquisaLookupCampo(Campos[i]).CampoChave + ' = ' + QuotedStr(TPesquisaLookupCampo(Campos[i]).cDBEdit.Text);

      //ShowMessage(sdsTemp.CommandText);
      sdsTemp.Open;
      if sdsTemp.IsEmpty then
      begin
        TFuncoesPesquisa.CriarFormPesquisa(TPesquisaLookupCampo(Campos[i]).Tabela,TPesquisaLookupCampo(Campos[i]).ApelidoTabela,TPesquisaLookupCampo(Campos[i]).CampoChave,TPesquisaLookupCampo(Campos[i]).CaptionFormPesquisa);
        if VarToStr(FrmPesquisaPadrao2.Pesquisa.CodRetorno) <> '' then
          begin
            //ShowMessage(VarToStr(FrmPesquisaPadrao2.Pesquisa.CampoRetorno));
            if not (TPesquisaLookupCampo(Campos[i]).cDBEdit.DataSource.State in [dsInsert,dsEdit])  then
              TPesquisaLookupCampo(Campos[i]).cDBEdit.DataSource.Edit;

            TPesquisaLookupCampo(Campos[i]).cDBEdit.Field.Text := VarToStr(FrmPesquisaPadrao2.Pesquisa.CodRetorno);
            sdsTemp.Close;
            sdsTemp.CommandText := 'select ' + TPesquisaLookupCampo(Campos[i]).CampoExibicao +
            ' from ' + TPesquisaLookupCampo(Campos[i]).Tabela + ' where ' +
            TPesquisaLookupCampo(Campos[i]).CampoChave + ' = ' + QuotedStr(TPesquisaLookupCampo(Campos[i]).cDBEdit.Text);
            sdsTemp.Open;
            TPesquisaLookupCampo(Campos[i]).cEdti.Text := sdsTemp.FieldByName(TPesquisaLookupCampo(Campos[i]).CampoExibicao).AsString;
          end
        else
        begin
          TPesquisaLookupCampo(Campos[i]).cEdti.Text := '';
          TPesquisaLookupCampo(Campos[i]).cDBEdit.DataSource.DataSet.FieldByName(
          TPesquisaLookupCampo(Campos[i]).cDBEdit.DataField).AsString := '';
        end;
      end
      else
      begin
        TPesquisaLookupCampo(Campos[i]).cEdti.Text := sdsTemp.FieldByName(TPesquisaLookupCampo(Campos[i]).CampoExibicao).AsString;
        TForm(TPesquisaLookupCampo(Campos[i]).cDBEdit.Owner).Perform(WM_NEXTDLGCTL,0,0);
      end;
    end;

  end;
  sdsTemp.Free;
  end;


end;

constructor TPesquisaLookup.Create;
begin
  Self.Campos := TObjectList.Create;
end;

destructor TPesquisaLookup.Destroy;
begin
  Self.Campos.Free;
  inherited;
end;



procedure TPesquisaLookup.EventoOnExitDBEdit(Sender: TObject);
var sdsTemp : TSQLQuery;
i : Integer;
a : TObject;
k : Char;
begin
  k := #13;
  a := Sender;
  sdsTemp := TSQLQuery.Create(nil);
  sdsTemp.SQLConnection := DM1.CONEXAO;
  for i := 0 to Campos.Count - 1 do
    begin
      if TPesquisaLookupCampo(Campos[i]).cDBEdit.Name = (Sender as TDBEdit).Name then
        if Trim(TPesquisaLookupCampo(Campos[i]).cDBEdit.Text) = '' then
          TPesquisaLookupCampo(Campos[i]).cEdti.Text := ''
        else
        begin
          sdsTemp.CommandText := 'select ' + TPesquisaLookupCampo(Campos[i]).CampoChave + ' , ' +
          TPesquisaLookupCampo(Campos[i]).CampoExibicao + ' from ' + TPesquisaLookupCampo(Campos[i]).Tabela + ' where ' +
          TPesquisaLookupCampo(Campos[i]).CampoChave + ' = ' + QuotedStr(TPesquisaLookupCampo(Campos[i]).cDBEdit.Text);
          //ShowMessage(sdsTemp.CommandText);
          sdsTemp.Open;
          if TPesquisaLookupCampo(Campos[i]).cDBEdit.Text <> sdsTemp.FieldByName(TPesquisaLookupCampo(Campos[i]).CampoChave).AsString then
            EventoKeyPressDBEdit(a,k)
          else
            TPesquisaLookupCampo(Campos[i]).cEdti.Text := sdsTemp.FieldByName(TPesquisaLookupCampo(Campos[i]).CampoExibicao).AsString;
      end;
    end;
  sdsTemp.Free;
end;

procedure TPesquisaLookup.PopularEDITs;
var i : Integer;
  qryAux : TSQLQuery;
begin
  qryAux := nil;
  try
    qryAux := TSQLQuery.Create(nil);
    qryAux.SQLConnection := DM1.CONEXAO;
    for i := 0 to Self.Campos.Count - 1 do
    begin
      if not TPesquisaLookupCampo(Self.Campos[i]).cDBEdit.Field.IsNull then
      begin
        qryAux.Close;
        qryAux.SQL.Text := 'select ' + TPesquisaLookupCampo(Self.Campos[i]).CampoExibicao +
        ' from ' + TPesquisaLookupCampo(Self.Campos[i]).Tabela + ' where ' +
        TPesquisaLookupCampo(Self.Campos[i]).CampoChave + ' = ' + QuotedStr(VarToStr(TPesquisaLookupCampo(Self.Campos[i]).cDBEdit.Field.Value));
        //ShowMessage(qryAux.SQL.Text);
        qryAux.Open;
        if not qryAux.IsEmpty then
          TPesquisaLookupCampo(Self.Campos[i]).cEdti.Text := qryAux.FieldByName(TPesquisaLookupCampo(Self.Campos[i]).CampoExibicao).AsString
        else
          TPesquisaLookupCampo(Self.Campos[i]).cEdti.Text := '';
      end
      else
        TPesquisaLookupCampo(Self.Campos[i]).cEdti.Text := '';
    end;

  finally
    qryAux.Free;
  end;


end;

procedure TPesquisaLookup.RegistrarEventos;
var i : Integer;
begin
  for i := 0 to Self.Campos.Count - 1 do
  begin
    if TPesquisaLookupCampo(Campos[i]).RegistraEventoOnKeyPress then
      TPesquisaLookupCampo(Campos[i]).cDBEdit.OnKeyPress := Self.EventoKeyPressDBEdit;
    if TPesquisaLookupCampo(Campos[i]).RegistraEventoOnExit then
      TPesquisaLookupCampo(Campos[i]).cDBEdit.OnExit := Self.EventoOnExitDBEdit;

  end;

end;

procedure TPesquisaLookup.SetCampos(const Value: TObjectList);
begin
  FCampos := Value;
end;


{ TFuncoesPesquisa }

class procedure TFuncoesPesquisa.CriarFormPesquisa(vTabela, vTabelaApelido,
  vCampoRetorno, vCaptionFormPesquisa: string);
begin
  if Assigned(FrmPesquisaPadrao2) then
  if FrmPesquisaPadrao2.Pesquisa.Tabela <> vTabela then
  begin
    FrmPesquisaPadrao2.Close;
    FreeAndNil(FrmPesquisaPadrao2);
  end;

  if not Assigned(FrmPesquisaPadrao2) then
  begin
    Application.CreateForm(TFrmPesquisaPadrao2,FrmPesquisaPadrao2);
    with FrmPesquisaPadrao2.Pesquisa do
    begin
      Tabela := vTabela;
      TabelaApelido := vTabelaApelido;
      CampoRetorno := vCampoRetorno;
      CaptionFormPesquisa := vCaptionFormPesquisa;
    end;
  end;
  FrmPesquisaPadrao2.Caption := vCaptionFormPesquisa;
  FrmPesquisaPadrao2.ShowModal;
end;

end.
